#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def add_line_numbers(input_file, output_file):
    """
    为输入文件的每一行添加行号，并写入输出文件
    
    Args:
        input_file: 输入文件路径
        output_file: 输出文件路径
    """
    try:
        with open(input_file, 'r', encoding='utf-8') as infile, \
             open(output_file, 'w', encoding='utf-8') as outfile:
            
            for line_num, line in enumerate(infile, 1):
                # 去除行尾的换行符，添加行号后再添加换行符
                outfile.write(f"{line_num}: {line}")
                
        print(f"已成功为文件 {input_file} 添加行号，结果保存在 {output_file}")
        
    except FileNotFoundError:
        print(f"错误：找不到文件 {input_file}")
    except Exception as e:
        print(f"处理文件时发生错误：{e}")

if __name__ == "__main__":
    input_file = "testfile.txt"
    output_file = "testfile_with_numbers.txt"
    
    add_line_numbers(input_file, output_file)